/*******************************************************************************
 * Copyright (c) 2011 squid.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     squid - initial API and implementation
 ******************************************************************************/
package com.google.code.cubeirc.ui;

import java.util.ArrayList;
import java.util.Iterator;

import lombok.Getter;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.wb.swt.SWTResourceManager;

import com.google.code.cubeirc.common.ApplicationInfo;
import com.google.code.cubeirc.common.Utils;
import com.google.code.cubeirc.config.ColorSettings;
import com.google.code.cubeirc.config.Configuration;
import com.google.code.cubeirc.dialogs.AlertBox;
import com.google.code.cubeirc.editor.Customs;

public class MessagesColorsForm extends Dialog  {

	protected Object result;
	protected Shell shlChangeEventsColors;
	@Getter
	private Table table;
	private Button btnSave;
	private Button btnClose;
	private TableColumn tblclmnEvent;
	private TableColumn tblclmnBackgroundColor;
	private TableColumn tblclmnForegroundColor;
	@Getter
	private Customs customs = new Customs();
	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public MessagesColorsForm(Shell parent, int style) {
		super(parent, style);
		setText(ApplicationInfo.APP_FULL + ":: Change events colors");
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		jbInit();
		this.shlChangeEventsColors.open();
		this.shlChangeEventsColors.layout();
		Display display = getParent().getDisplay();
		while (!this.shlChangeEventsColors.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return this.result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void jbInit() {
		this.shlChangeEventsColors = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		this.shlChangeEventsColors.setSize(545, 381);
		this.shlChangeEventsColors.setText("Change events colors");
		this.shlChangeEventsColors.setLayout(new FormLayout());
		
		this.table = new Table(this.shlChangeEventsColors, SWT.BORDER | SWT.FULL_SELECTION);
		FormData fd_table = new FormData();
		fd_table.left = new FormAttachment(0);
		fd_table.top = new FormAttachment(0);
		fd_table.right = new FormAttachment(0, 394);
		this.table.setLayoutData(fd_table);
		this.table.setHeaderVisible(true);
		
		this.btnSave = new Button(this.shlChangeEventsColors, SWT.NONE);
		this.btnSave.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				SaveTable();
			}
		});
		this.btnSave.setImage(SWTResourceManager.getImage(MessagesColorsForm.class, "/com/google/code/cubeirc/resources/img_save.png"));
		FormData fd_btnSave = new FormData();
		fd_btnSave.left = new FormAttachment(this.table, 10);
		fd_btnSave.right = new FormAttachment(100, -10);
		fd_btnSave.top = new FormAttachment(0, 10);
		this.btnSave.setLayoutData(fd_btnSave);
		this.btnSave.setText("Save");
		
		this.btnClose = new Button(this.shlChangeEventsColors, SWT.NONE);
		this.btnClose.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				shlChangeEventsColors.dispose();
			}
		});
		fd_table.bottom = new FormAttachment(this.btnClose, 0, SWT.BOTTOM);
		
		this.tblclmnEvent = new TableColumn(this.table, SWT.NONE);
		this.tblclmnEvent.setWidth(122);
		this.tblclmnEvent.setText("Event");
		
		this.tblclmnBackgroundColor = new TableColumn(this.table, SWT.NONE);
		this.tblclmnBackgroundColor.setWidth(124);
		this.tblclmnBackgroundColor.setText("Background color");
		
		this.tblclmnForegroundColor = new TableColumn(this.table, SWT.NONE);
		this.tblclmnForegroundColor.setWidth(128);
		this.tblclmnForegroundColor.setText("Foreground color");
		this.btnClose.setImage(SWTResourceManager.getImage(MessagesColorsForm.class, "/com/google/code/cubeirc/resources/img_exit.png"));
		FormData fd_btnClose = new FormData();
		fd_btnClose.left = new FormAttachment(0, 404);
		fd_btnClose.right = new FormAttachment(100, -10);
		fd_btnClose.top = new FormAttachment(100, -35);
		fd_btnClose.bottom = new FormAttachment(100, -10);
		this.btnClose.setLayoutData(fd_btnClose);
		this.btnClose.setText("Close");
		initTable();
		
		

	}
	
	private void initTable()
	{ 
		asyncExec(new Runnable() {
			
			@Override
			public void run() {
			   for(Iterator<ColorSettings> it = Configuration.getCurrentColorsSettings().iterator(); it.hasNext();)
			   {
				 addItemTable(it.next());  
			   }
			}
		});		
		
	}
	
	private void addItemTable(ColorSettings cs)
	{
		CCombo cb_f = getColorsCombo();
		CCombo cb_b = getColorsCombo();
		TableItem ti = new TableItem(getTable(), SWT.BORDER);
		TableEditor editor = new TableEditor(getTable());
	
		editor.grabHorizontal =editor.grabVertical = true;
		
		editor.minimumWidth = cb_f.getSize().x;
		ti.setText(0, cs.getName());
		editor.grabHorizontal =editor.grabVertical = true;
		editor.setEditor(cb_f, ti, 1);
		editor = new TableEditor(getTable());
		editor.setEditor(cb_b, ti, 2);
		editor.grabHorizontal =editor.grabVertical = true;
		cb_f.pack();
		cb_b.pack();
		Object[] combos = {cb_b, cb_f};
		
		cb_f.select(cs.getForeground()+1);
		cb_b.select(cs.getBackground()+1);
		ti.setData(combos); 
				
	}
	
	private void SaveTable()
	{
		asyncExec(new Runnable() {
			
			@Override
			public void run() {
				Configuration.setCurrentColorsSettings(new ArrayList<ColorSettings>());
				TableItem[] tis = getTable().getItems();
				for (int i=0;i<tis.length;i++)
				{
					Object[] objs = (Object[])tis[i].getData();
					CCombo cb_b = ((CCombo)(objs[0]));
					CCombo cb_f = ((CCombo)(objs[1]));
				
					
					Configuration.getCurrentColorsSettings().add(new ColorSettings(tis[i].getText(0), Integer.parseInt(cb_b.getData(cb_b.getText()).toString()) , Integer.parseInt(cb_f.getData(cb_f.getText()).toString())));					
				}
				Configuration.saveColorsSettings(ApplicationInfo.CLS_COLORS, Configuration.getCurrentColorsSettings());
				new AlertBox(shlChangeEventsColors, getText(), "Colors settings saved!", SWT.ICON_INFORMATION);
				shlChangeEventsColors.dispose();
			}
		});

	}
	
	
	
	private void asyncExec(Runnable runnable)
	{
		Utils.asyncExec(getParent().getDisplay(), runnable);
	}
	
	private CCombo getColorsCombo()
	{
		CCombo cb = new CCombo(getTable(), SWT.NONE);
		cb.add("Normal");
		cb.setData("Normal", -1);
		
		for(String key :customs.ircColors.keySet() )
		{
			cb.add(key);
			cb.setData(key, customs.ircColors.get(key));
		}
		
		return cb;
	}
}
